package com.kmxd.ams.core.common.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.exception.BizException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

/**
 * 字段排序方式
 *
 * @author nickbi
 */
@Getter
@AllArgsConstructor
public enum FieldOrderTypeEnum {
  /** 升序 */
  ASC("asc", "升序"),
  /** 降序 */
  DESC("desc", "降序");

  private String type;
  private String desc;

  public static FieldOrderTypeEnum convert(String type) {
    return Arrays.stream(values())
        .filter(x -> x.type.equals(type))
        .findAny()
        .orElseThrow(() -> BizException.instance(ErrorCode.NOT_FOUND, "未找到排序类型：" + type));
  }

  @JsonCreator
  public static FieldOrderTypeEnum getValue(String code) {
    return Arrays.stream(values()).filter(x -> Objects.equals(x.type, code)).findAny().orElse(null);
  }
}
